package com.hernandez.games.adventures.controllers;

import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

import com.hernandez.games.adventures.beans.Resolution;
import com.hernandez.games.adventures.beans.Window;
import com.hernandez.games.adventures.events.GameStatusChangedEvent;
import com.hernandez.games.adventures.events.IEventHandler;
import com.hernandez.games.adventures.views.GameCanvas;
import com.hernandez.games.adventures.views.ICanvas;

public class ViewController implements IController, IEventHandler<GameStatusChangedEvent> {
	private static final Logger LOG = Logger.getLogger(ViewController.class.getSimpleName());
	private static final String VIEW_CONFIG = "/resources/view.properties";
	private ICanvas gameCanvas;
	private Window window;
	public ViewController() {
		EventController.getInstance().registerHandler(GameStatusChangedEvent.class, this);
		Properties prop = new Properties();
		try {
			prop.load(getClass().getResourceAsStream(VIEW_CONFIG));
		} catch (IOException e) {
			LOG.severe("Couldn't read '"+VIEW_CONFIG+"' file");
		}
		
		int width = Integer.valueOf(prop.getProperty("window.width"));
		int height = Integer.valueOf(prop.getProperty("window.height"));
		window = new Window(prop.getProperty("window.title"), new Resolution(width, height));
	}

	@Override
	public void publishEvent(GameStatusChangedEvent event) {
		switch(event.getGameStatus()) {
		case PREPARING:
			gameCanvas = new GameCanvas(window);
			break;
		case STARTED:
			gameCanvas.show();
			break;
		case EXITING:
			gameCanvas.dispose();
			break;
		}
	}

}
